package com.zjjt.dc.tools.upload.file.config;

import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.Map;

public class AliOssBucketConfig {
    private static AliOssBucketConfig instance;
    private Map<String, Object> config;
    private String ossName;

    private AliOssBucketConfig(String name) {
        this.ossName = name;
        loadConfig();
    }

    public static AliOssBucketConfig newInstance(String name) {
        if (instance == null) {
            instance = new AliOssBucketConfig(name);
        }
        return instance;
    }
    @SuppressWarnings("unchecked")
    private void loadConfig() {
        try {
            Yaml yaml = new Yaml();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/tools/ali_oss.yml");
            if (inputStream != null) {
                config = yaml.load(inputStream);
            }
        } catch (Exception e) {
            e.printStackTrace();
            // 如果加载失败，使用默认配置
            config = null;
        }
    }

    @SuppressWarnings("unchecked")
    public String getEndpoint() {
        if (config != null) {
            Map<String, Object> ossObj = (Map<String, Object>) config.get("oss");
            if (ossObj != null) {
                Map<String, Object> ossConf = (Map<String, Object>) ossObj.get(this.ossName);
                Object result = ossConf.get("endpoint");
                return result.toString();
            }
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    public String getSecretKey() {
        if (config != null) {
            Map<String, Object> ossObj = (Map<String, Object>) config.get("oss");
            if (ossObj != null) {
                Map<String, Object> ossConf = (Map<String, Object>) ossObj.get(this.ossName);
                Object result = ossConf.get("secretKey");
                return result.toString();
            }
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    public String getAccessKey() {
        if (config != null) {
            Map<String, Object> ossObj = (Map<String, Object>) config.get("oss");
            if (ossObj != null) {
                Map<String, Object> ossConf = (Map<String, Object>) ossObj.get(this.ossName);
                Object result = ossConf.get("accessKey");
                return result.toString();
            }
        }
        return "";
    }

    @SuppressWarnings("unchecked")
    public String getBucketName() {
        if (config != null) {
            Map<String, Object> ossObj = (Map<String, Object>) config.get("oss");
            if (ossObj != null) {
                Map<String, Object> ossConf = (Map<String, Object>) ossObj.get(this.ossName);
                Object result = ossConf.get("bucketName");
                return result.toString();
            }
        }
        return "";
    }


}
